package de.axiscars;

import java.sql.*;

/**
 * This class consists of a method for connecting to database.
 * 
 * @author Sabrina Schwarze
 * @author Ramiz Loki
 * @author Hendrik Bielefeld
 *
 */
public class Database {
	  
	  /**
	   * Creates a connection to the database.
	   * @return An object of type java.sql.Connection
	   */
	  public Connection connect() {
		  
		//define some configurations
	    String url = "jdbc:mysql://localhost:3306/saherace";
	    String driver = "com.mysql.jdbc.Driver";
	    String usr = "root";
	    String pwd = "";
	    
	    //Load db driver
	    Connection result = null;
	    try {
	       Class.forName(driver).newInstance();
	    }
	    catch (Exception ex){
	       log("Check classpath. Cannot load db driver: " + driver);
	    }
	    
	    //Connect to database
	    try {
	      result = DriverManager.getConnection(url, usr, pwd);
	    }
	    catch (SQLException e){
	       log( "Driver loaded, but cannot connect to db: " + url);
	    }
	    return result;
	  }

	  /**
	   * This methods log some actions
	   * @param aObject The object that should be logged
	   */
	  private static void log(Object aObject){
	    System.out.println(aObject);
	  }
}
			

